home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Newswatcher 2.0b22 / NW Source / Source / status.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-18  |  7.4 KB  |  315 lines  |  [TEXT/MMCC]

  1. /*----------------------------------------------------------------------------
  2.  
  3.     status.c
  4.  
  5.     This module handles the status window.
  6.     
  7.     Copyright © 1994, Northwestern University.
  8.  
  9. ----------------------------------------------------------------------------*/
  10.  
  11. #include <string.h>
  12. #include <Script.h>
  13.  
  14. #include "glob.h"
  15. #include "status.h"
  16. #include "newswatcher.h"
  17. #include "dialog.h"
  18. #include "menus.h"
  19. #include "strutil.h"
  20. #include "windutil.h"
  21. #include "memutil.h"
  22. #include "wind.h"
  23.  
  24.  
  25.  
  26. #define kWindowWidth    450                /* status window width */
  27. #define kWindowHeight    80                /* status window height */
  28.  
  29.  
  30.  
  31. static Str255 gStatusMsg;            /* currently displayed status message */
  32.  
  33.  
  34.  
  35. /*----------------------------------------------------------------------------
  36.     DisplayStatusMessage 
  37.     
  38.     Display a status message during a long operation.
  39.     
  40.     Entry:    msg = status message.
  41.     
  42.     Exit:    function result = error code.
  43. ----------------------------------------------------------------------------*/
  44.  
  45. OSErr DisplayStatusMessage (char *msg)
  46. {
  47.     WindowPtr wind = nil;
  48.     GrafPtr port;
  49.     Rect r;
  50.     Str255 title, buttonName, fontName;
  51.     OSErr err = noErr;
  52.     Boolean mainScreen, onOneScreen;
  53.     
  54.     GetPort(&port);
  55.     wind = MyFrontWindow();
  56.     
  57.     if (GetWindowKind(wind) != kStatus) {
  58.         GetPString(kStrStatusWindowTitle, title);
  59.         GetFontName(systemFont, fontName);
  60.         err = CreateNewWindow(kStatus, title, fontName, 12, &wind);
  61.         if (err != noErr) goto exit;
  62.         MoveWindow(wind, gPrefs.statusWindowLocn.h, gPrefs.statusWindowLocn.v, false);
  63.         SizeWindow(wind, kWindowWidth, kWindowHeight, false);
  64.         if (!WindOnScreen(wind)) {
  65.             GetDominantScreen(nil, &r, &mainScreen, &onOneScreen);
  66.             MoveWindow(wind, (r.left + r.right - wind->portRect.right) >> 1, 
  67.                 GetMBarHeight() + 40, false);
  68.         }
  69.         SetRect(&r, kWindowWidth-70, kWindowHeight-30, kWindowWidth-10, kWindowHeight-10);
  70.         GetPString(kStrStatusCancelButtonName, buttonName);
  71.         NewControl(wind, &r, buttonName, true, 0, 0, 0, pushButProc, 0);
  72.         MyShowWindow(wind);
  73.     } else {
  74.         SetPort(wind);
  75.         SetRect(&r, 10, 10, kWindowWidth-10, 40);
  76.         InvalRect(&r);
  77.     }
  78.     
  79.     strcpy((char*)gStatusMsg, msg);
  80.     c2pstr((char*)gStatusMsg);
  81.     HandleUpdate(wind);
  82.     SetMenusTo(kAppleOnlyAboutDisabled, 0, 0, 0, 0, 0);
  83.     GiveTime(false);
  84.     SetPort(port);
  85.     return noErr;
  86.     
  87. exit:
  88.  
  89.     DoClose(wind);
  90.     SetPort(port);
  91.     return err;
  92. }
  93.  
  94.  
  95.  
  96. /*----------------------------------------------------------------------------
  97.     DisplayStatusMessageNumber 
  98.     
  99.     Display a status message during a long operation.
  100.     
  101.     Entry:    index = index in STR# 128 resource of status message.
  102.     
  103.     Exit:    function result = error code.
  104. ----------------------------------------------------------------------------*/
  105.  
  106. OSErr DisplayStatusMessageNumber (short index)
  107. {
  108.     CStr255 msg;
  109.     
  110.     GetCString(index, msg);
  111.     return DisplayStatusMessage(msg);
  112. }
  113.  
  114.  
  115.  
  116. /*----------------------------------------------------------------------------
  117.     Activate 
  118.     
  119.     Handle an activate event for the status window.
  120.             
  121.     Entry:    wind = pointer to status window.
  122.             act = true to activate, false to deactivate
  123. ----------------------------------------------------------------------------*/
  124.  
  125. static void Activate (WindowPtr wind, Boolean act)
  126. {
  127. }
  128.  
  129.  
  130.  
  131. /*----------------------------------------------------------------------------
  132.     Update 
  133.     
  134.     Handle an update event for the status window.
  135.             
  136.     Entry:    wind = pointer to status window.
  137. ----------------------------------------------------------------------------*/
  138.  
  139. static void Update (WindowPtr wind)
  140. {
  141.     UpdateControls(wind, wind->visRgn);
  142.     TextFont(systemFont);
  143.     TextSize(12);
  144.     TruncString(kWindowWidth-20, gStatusMsg, smTruncEnd);
  145.     MoveTo(10, 29);
  146.     DrawString(gStatusMsg);
  147.     
  148.     #ifdef kDevelopmentVersion
  149.         {
  150.             char *t1 = "NewsWatcher development version.";
  151.             char *t2 = "Please do not redistribute.";
  152.         
  153.             TextFont(applFont);
  154.             TextSize(9);
  155.             MoveTo(10, 60);
  156.             DrawText(t1, 0, strlen(t1));
  157.             MoveTo(10, 71);
  158.             DrawText(t2, 0, strlen(t2));
  159.         }
  160.     #endif
  161.  
  162. }
  163.  
  164.  
  165.  
  166. /*----------------------------------------------------------------------------
  167.     Mouse 
  168.     
  169.     Handle a mouse down event in the content area of the status window.
  170.             
  171.     Entry:    wind = pointer to status window.
  172.             where = location of mouse down in local coords.
  173.             modifiers = modifiers field from event record.
  174.             
  175.     Exit:    function result = error code.
  176. ----------------------------------------------------------------------------*/
  177.  
  178. static OSErr Mouse (WindowPtr wind, Point where, short modifiers)
  179. {
  180.     short part;
  181.     ControlHandle control;
  182.  
  183.     part = FindControl(where, wind, &control);
  184.     if (part  != 0) {
  185.         if (TrackControl(control, where, nil) != 0) gCancel = true;
  186.     }
  187.     return noErr;
  188. }
  189.  
  190.  
  191.  
  192. /*----------------------------------------------------------------------------
  193.     Draggable
  194.     
  195.     Determine whether a mouse down event is on a draggable object in a 
  196.     status window.
  197.     
  198.     Entry:    wind = pointer to status window.
  199.             where = location of mouse down event, in local coordinates.
  200.             modifiers = modifiers field from event record.
  201.             
  202.     Exit:    function result = true if object is draggable.
  203. ----------------------------------------------------------------------------*/
  204.  
  205. static Boolean Draggable (WindowPtr wind, Point where, short modifiers)
  206. {
  207.     return false;
  208. }
  209.  
  210.  
  211.  
  212. /*----------------------------------------------------------------------------
  213.     Key 
  214.     
  215.     Handle a key down event for the status window.
  216.             
  217.     Entry:    wind = pointer to status window.
  218.             theChar = ASCII code of key.
  219.             theKey = keyboard code of key.
  220.             modifiers = modifiers field from event record.
  221.             
  222.     Exit:     function result = error code.
  223. ----------------------------------------------------------------------------*/
  224.  
  225. static OSErr Key (WindowPtr wind, unsigned char theChar, unsigned char theKey, short modifiers)
  226. {
  227.     ControlHandle cancelButton;
  228.     long ticks;
  229.  
  230.     if (theKey == escapeKeyCode || (modifiers & cmdKey) != 0 && theChar == '.') {
  231.         cancelButton = ((WindowPeek)wind)->controlList;
  232.         HiliteControl(cancelButton, 1);
  233.         Delay(8, &ticks);
  234.         HiliteControl(cancelButton, 0);
  235.         gCancel = true;
  236.         return noErr;
  237.     }
  238.     SysBeep(0);
  239.     return noErr;
  240. }
  241.  
  242.  
  243.  
  244. /*----------------------------------------------------------------------------
  245.     Command 
  246.     
  247.     Handle a command for the status window.
  248.             
  249.     Entry:    wind = pointer to status window.
  250.             menu = the menu.
  251.             item = the item.
  252.             modifiers = modifiers field from event record.
  253.     
  254.     Exit:    function result = error code.
  255. ----------------------------------------------------------------------------*/
  256.  
  257. static OSErr Command (WindowPtr wind, short menu, short item, short modifiers)
  258. {
  259.     return noErr;
  260. }
  261.  
  262.  
  263.  
  264. /*----------------------------------------------------------------------------
  265.     Close 
  266.     
  267.     Close the status window.
  268.             
  269.     Entry:    wind = pointer to status window.
  270.     
  271.     Exit:    function result = error code.
  272. ----------------------------------------------------------------------------*/
  273.  
  274. static OSErr Close (WindowPtr wind)
  275. {
  276.     TWindow **info;
  277.     GrafPtr port;
  278.     
  279.     GetPort(&port);
  280.     SetPort(wind);
  281.     SetPt(&gPrefs.statusWindowLocn, 0, 0);
  282.     LocalToGlobal(&gPrefs.statusWindowLocn);
  283.     info = (TWindow**)GetWRefCon(wind);
  284.     MyDisposeHandle(info);
  285.     MyDisposeWindow(wind);
  286.     SetPort(port);
  287.     return noErr;
  288. }
  289.  
  290.  
  291.  
  292. /*----------------------------------------------------------------------------
  293.     InitStatusDispatchTable 
  294.     
  295.     Initialize the dispatch table for the status window.
  296. ----------------------------------------------------------------------------*/
  297.  
  298. void InitStatusDispatchTable (void)
  299. {
  300.     TDispatch *d;
  301.     
  302.     d = &gDispatch[kStatus];
  303.     
  304.     d->activate = Activate;
  305.     d->update = Update;
  306.     d->mouse = Mouse;
  307.     d->draggable = Draggable;
  308.     d->key = Key;
  309.     d->grow = nil;
  310.     d->zoom = nil;
  311.     d->command = Command;
  312.     d->close = Close;
  313.     d->idle = nil;
  314. }
  315.